package cn.wolfcode.luowowo.flight.service.impl;

import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.flight.domain.Flight;
import cn.wolfcode.luowowo.flight.domain.FlightTicket;
import cn.wolfcode.luowowo.flight.mapper.FlightMapper;
import cn.wolfcode.luowowo.flight.query.FlightQueryObject;
import cn.wolfcode.luowowo.flight.service.IFlightService;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;

import java.util.*;

@Service
public class FlightServiceImpl implements IFlightService {
    @Autowired
    private FlightMapper flightMapper;

    @Override
    public List<Flight> selectByHotTop10(FlightQueryObject qo) {
        List<Flight> list = flightMapper.selectByHotTop10(qo);
        List list1 = removeDuplicate(list);
        return list1;
    }
    //去重复
    private static List removeDuplicate(List<Flight> list) {
        for (int i = 0; i < list.size() - 1; i++) {
            for (int j = list.size() - 1; j > i; j--) {
                if (list.get(j).getName().equals(list.get(i).getName())) {
                    list.remove(j);
                }
            }
        }
        List<Flight> l = new ArrayList<>();
        for (Flight flight : list) {
            if(flight.getName().trim().equals("")){
               continue;
            }
            l.add(flight);
        }
        return l;
    }

    @Override
    public List<Flight> selectByInitialABCDE() {
        return removeDuplicate(flightMapper.selectByInitialABCDE());
    }

    @Override
    public List<Flight> selectByInitialFGHJ() {
        return removeDuplicate(flightMapper.selectByInitialFGHJ());
    }

    @Override
    public List<Flight> selectByInitialKLMNP() {
        return removeDuplicate(flightMapper.selectByInitialKLMNP());
    }

    @Override
    public List<Flight> selectByInitialQRSTW() {
        return removeDuplicate(flightMapper.selectByInitialQRSTW());
    }

    @Override
    public List<Flight> selectByInitialXYZ() {
        return removeDuplicate(flightMapper.selectByInitialXYZ());
    }

    @Override
    public AjaxResult selectByPortAndTime(FlightQueryObject qo) {
        AjaxResult result = new AjaxResult();
        try {
            //检验查询条件是否为空
            /*Assert.hasLength(qo.getOrgCity(), "出发城市不能为空");
            Assert.hasLength(qo.getDstCity(), "到达城市不能为空");*/

            List<Flight> flightInfos = flightMapper.selectByPortAndTime(qo);
            String s = JSON.toJSONString(flightInfos);
            result.addData(s);

        } catch (Exception e) {
            e.printStackTrace();
            AjaxResult.FAIL.setMsg(e.getMessage());
        }

        return result;
    }

    @Override
    public Flight queryById(Long tid) {

        return flightMapper.selectByPrimaryKey(tid);
    }
}
